package com.yvdelx.cyclowomen.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.yvdelx.R;
import com.yvdelx.cyclowomen.mediation.CycleManagement;
import com.yvdelx.cyclowomen.utility.ActivityUtility;
import com.yvdelx.cyclowomen.utility.LogUtility;

/**
 * Main activity instantiate the app
 */
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityUtility.addActivity(this);

        try {
            Intent intent;

            // If it's the first use of the app, the user need to complete his profile
            if(CycleManagement.isFirstUse()){
                LogUtility.debug(this.getClass(),"First use of the application.");
                intent = new Intent(this, ProfileActivity.class);
                startActivityForResult(intent, ActivityUtility.CST_PROFILE_ACTIVITY);
            } else {
                LogUtility.debug(this.getClass(),"The backup file exist !");
                try {
                    CycleManagement.getProfile();
                    intent = new Intent(this, CalendarActivity.class);
                    startActivityForResult(intent, ActivityUtility.CST_CALENDAR_ACTIVITY);
                } catch (Exception ex){
                    LogUtility.error(this.getClass(), ex.getMessage(), ex);
                    this.finish();
                }
            }
        } catch (Exception e){
            this.finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // All activities return here, this activity close the app.
        this.finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return item.getItemId() == R.id.action_settings || super.onOptionsItemSelected(item);
    }

}
